<template>
  <div class="search-history">
    <van-cell title="搜索历史" >
      <div v-if="isDeleteShow">
        <span @click="$emit('clearSearchHistories')">全部删除</span>&emsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else name="delete" @click="isDeleteShow = true" />
    </van-cell>
    <van-cell
    :title="item"
    v-for="(item,index) in searchHistories"
    :key="index"
    @click="onSearchItemClick(item,index)" >
      <van-icon name="close" v-show="isDeleteShow" />
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'searchHistory',
  components: {},
  // 如果props的值是一个引用数据类型，那么只要不对它重新赋值，实际上地址值没有改变不会影响
  // prop 是受父组件数据影响
  // 1 如果是普通数据（数字，字符串，布尔值）绝对不能修改即便修改了也无法传导给父组件
  // 2 如果是引用数据类型（数组，对象）可以修改内容，列如[].push 但是不能重新赋值,xxx = []
  props: {
    searchHistories: {
      type: Array,
      reqiured: true
    }
  },
  data() {
    return {
      isDeleteShow: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onSearchItemClick(item, index) {
      if (this.isDeleteShow) {
        // 删除状态
        this.searchHistories.splice(index, 1)
      } else {
        // 非删除状态
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style lang="less" scoped></style>
